using System;
namespace MicroMsgBackground.Protocol
{
	public class TLVUtil
	{
		public static void fillByteArray(byte[] array, byte byte_0)
		{
			for (int i = 0; i < array.Length; i++)
			{
				array[0] = byte_0;
			}
		}
		public static int byte2int(byte[] byte_0, int offSet, int int_0, bool isBigHead)
		{
			if (int_0 == 4)
			{
				if (isBigHead)
				{
					return (int)byte_0[offSet] << 24 | (int)(byte_0[offSet + 1] & 255) << 16 | (int)(byte_0[offSet + 2] & 255) << 8 | (int)(byte_0[offSet + 3] & 255);
				}
				return (int)byte_0[offSet + 3] << 24 | (int)(byte_0[offSet + 2] & 255) << 16 | (int)(byte_0[offSet + 1] & 255) << 8 | (int)(byte_0[offSet] & 255);
			}
			else
			{
				if (isBigHead)
				{
					return (int)byte_0[offSet] << 8 | (int)(byte_0[offSet + 1] & 255);
				}
				return (int)byte_0[offSet + 1] << 8 | (int)(byte_0[offSet] & 255);
			}
		}
		public static byte[] int2byte(int int_0, int int_1, bool isBigHead)
		{
			byte[] array;
			if (int_1 == 4)
			{
				array = new byte[4];
				if (isBigHead)
				{
					array[0] = (byte)(int_0 >> 24 & 255);
					array[1] = (byte)(int_0 >> 16 & 255);
					array[2] = (byte)(int_0 >> 8 & 255);
					array[3] = (byte)(int_0 & 255);
				}
				else
				{
					array[3] = (byte)(int_0 >> 24 & 255);
					array[2] = (byte)(int_0 >> 16 & 255);
					array[1] = (byte)(int_0 >> 8 & 255);
					array[0] = (byte)(int_0 & 255);
				}
			}
			else
			{
				array = new byte[2];
				if (isBigHead)
				{
					array[0] = (byte)(int_0 & 255);
					array[1] = (byte)(int_0 >> 8 & 255);
				}
				else
				{
					array[1] = (byte)(int_0 & 255);
					array[0] = (byte)(int_0 >> 8 & 255);
				}
			}
			return array;
		}
		public static int EncodeVByte32(int auValue, byte[] apcBuffer, int int_0)
		{
			int result = 0;
			while (auValue >= 128)
			{
				apcBuffer[int_0 + result++] = (byte)(128 + (auValue & 127));
				auValue >>= 7;
			}
			apcBuffer[int_0 + result++] = (byte)auValue;
			return result;
		}
		public static int DecodeVByte32(ref int apuValue, byte[] apcBuffer, int int_0)
		{
			int num = 0;
			int num2 = 0;
			int expr_09 = 0;
			int result = expr_09 + 1;
			byte b = apcBuffer[int_0 + expr_09];
			int num3;
			while ((b & 255) >= 128)
			{
				num3 = (int)(b & 127);
				num2 += num3 << num;
				num += 7;
				b = apcBuffer[int_0 + result++];
			}
			num3 = (int)b;
			num2 += num3 << num;
			apuValue = num2;
			return result;
		}
		public static bool IsLittleEndianMechine()
		{
			return BitConverter.IsLittleEndian;
		}
		public static byte[] ToLittleEndian(short short_0)
		{
			return new byte[]
			{
				(byte)(short_0 & 255),
				(byte)(short_0 >> 8 & 255)
			};
		}
		public static byte[] ToLittleEndian(int int_0)
		{
			byte[] array = new byte[2];
			array[0] = (byte)(int_0 & 255);
			array[1] = (byte)(int_0 >> 8 & 65280);
			array[2] = (byte)(int_0 >> 16 & 255);
			array[3] = (byte)(int_0 >> 24 & 65280);
			return array;
		}
		public static byte[] getCheckSum(byte[] apcBuf, int offset, int aiSize)
		{
			byte[] array = new byte[2];
			if (apcBuf != null && aiSize != 0)
			{
				ushort num = 0;
				for (int i = 0; i < aiSize; i++)
				{
					num += (ushort)((sbyte)apcBuf[offset + i]);
				}
				uint num2 = (uint)num;
				num2 = ~num2;
				num = (ushort)(num2 & 65535u);
				array[0] = (byte)(num & 255);
				array[1] = (byte)(num >> 8 & 255);
				return array;
			}
			array[0] = 255;
			array[1] = 255;
			return array;
		}
	}
}
